/**
 *
 */
package com.freedom.study.service.impl;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.freedom.study.comresp.RespApiEnum;
import com.freedom.study.comresp.RespCodeEnum;
import com.freedom.study.comresp.ReturnResponse;
import com.freedom.study.model.EsReq;
import com.freedom.study.service.IEsService;
import com.freedom.study.util.EsClientUtil;

/**
 * 描述：实现最基本的增加删除改查
 * @author LiPan
 * @since 2017年11月15日下午1:07:55
 */
@Service
public class EsProServiceImpl implements IEsService{

	public ReturnResponse checkParameter(String article, String author) {
		if (StringUtils.isBlank(article)||StringUtils.isBlank(author)) {
			return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SYS_ERROR_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SYS_ERROR_LOCAL.getInfo(),"文章标题或者作者不能为空");
		}
		return null;
	}
	
	/**
	 * 获取数据
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	@Override
	public ReturnResponse getEsSimpleData(String article, String author) throws UnknownHostException, IOException {
		ReturnResponse response = checkParameter(article, author);
		if (null!=response) {
			return response;
		}
		Map<String, Object> esData = EsClientUtil.getEsData(article, author);
		if (null!=esData) {
			return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SUCCESS_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SUCCESS_LOCAL.getInfo(),esData);
		}else {
			return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SYS_ERROR_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SYS_ERROR_LOCAL.getInfo(),"获取信息失败");
		}
	}

	/**
	 * 新加数据
	 */
	@Override
	public ReturnResponse addEsSimpleData(EsReq req) {
		ReturnResponse response = checkParameter(req.getArticle(), req.getAuthor());
		if (null!=response) {
			return response;
		}
		 boolean flag = EsClientUtil.putEsData(JSONObject.toJSONString(req), req.getArticle(), req.getAuthor());
		 if (flag) {
			 return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SUCCESS_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SUCCESS_LOCAL.getInfo(),"存储成功");
		}else {
			return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SYS_ERROR_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SYS_ERROR_LOCAL.getInfo(),"存储失败");
		}
	}

	@Override
	public ReturnResponse delEsSimpleData(String article, String author) {
		ReturnResponse response = checkParameter(article, author);
		if (null!=response) {
			return response;
		}
		boolean flag = EsClientUtil.deleteEsData(article, author);
		 if (flag) {
			 return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SUCCESS_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SUCCESS_LOCAL.getInfo(),"删除成功");
		}else {
			return new ReturnResponse(RespApiEnum.ESXX002.getCode(), RespCodeEnum.SYS_ERROR_LOCAL.getCode(), RespApiEnum.ESXX002.getInfo(), RespCodeEnum.SYS_ERROR_LOCAL.getInfo(),"删除失败");
		}
	}

}
